﻿@page "/news"
@using Blazorise.Docs.BlogRuntime
@inject Blazorise.Docs.BlogRuntime.IBlogProvider BlogProvider

<Seo Canonical="/news" Title="Blazorise News" Description="Read latest news about the Blazorise UI controls, Megabit company details, releases, bug fixes, and more." ImageUrl="@NewsEntries?.FirstOrDefault( x => x.Pinned )?.ImageUrl" />

<BlogPageTitle>
    Blazorise News
</BlogPageTitle>

<Row>
    <Column ColumnSize="ColumnSize.Is8.OnDesktop">
        <BlogCategorySelect Categories="@NewsEntries.Select( x => x.Category ).Distinct()" @bind-SelectedCategory="@selectedCategory" />
    </Column>
    <Column ColumnSize="ColumnSize.Is4.OnDesktop" Margin="Margin.Is3.FromBottom">
        <TextEdit Placeholder="Search..." @bind-Text="@search" />
    </Column>
</Row>

@if ( !IsCustomSearch )
{
    <Row>
        <Column ColumnSize="ColumnSize.Is8.OnDesktop">
            @{
                var pinnedBlog = NewsEntries.FirstOrDefault( x => x.Pinned );

                if ( pinnedBlog == null )
                    pinnedBlog = NewsEntries.FirstOrDefault();

                if ( pinnedBlog != null )
                {
                    <Card Margin="Margin.Is4.FromBottom" Shadow="Shadow.Default" Border="Border.Is0">
                        <CardImage Source="@pinnedBlog.ImageUrl" Alt="Blog post image"></CardImage>
                        <CardBody Padding="Padding.Is0.FromBottom">
                            <CardTitle>
                                @if ( pinnedBlog.Permalink != null )
                                {
                                    <Anchor To="@pinnedBlog.Permalink" Stretched>
                                        @pinnedBlog.Summary
                                    </Anchor>
                                }
                            </CardTitle>
                        </CardBody>
                        <CardBody Padding="Padding.Is0.OnY">
                            <BlogPagePostInfo UserName="@pinnedBlog.AuthorName" ImageName="@pinnedBlog.AuthorImage" PostedOn="@pinnedBlog.PostedOn" Read="@pinnedBlog.ReadTime" />
                        </CardBody>
                    </Card>
                }
            }
        </Column>
        <Column ColumnSize="ColumnSize.Is4.OnDesktop">
            <NewsletterWidget />

            <LatestBlogItems BlogItems="@NewsEntries" />
        </Column>
    </Row>
}

<BlogPageEntries Year="2022">
    @foreach ( var item in NewsEntries.Where( x => ( ( selectedCategory == "All" || !string.IsNullOrEmpty( search ) ) || x.Category == selectedCategory ) && x.Summary.Contains( search ?? string.Empty, StringComparison.InvariantCultureIgnoreCase ) ) )
    {
        <BlogPageEntriesItem @key="@item" To="@item.Permalink" ToText="@item.Title" ImageSource="@item.ImageUrl" AuthorName="@item.AuthorName" AuthorImage="@item.AuthorImage" PostedOn="@item.PostedOn" ReadTime="@item.ReadTime" />
    }
</BlogPageEntries>

@code {
    private IReadOnlyList<BlogIndexItem> NewsEntries = new List<BlogIndexItem>();
    bool IsCustomSearch => selectedCategory != "All" || !string.IsNullOrEmpty( search );
    string search = "";
    string selectedCategory = "All";

    protected override async Task OnInitializedAsync()
    {
        NewsEntries = await BlogProvider.GetListAsync( "news" );
    }
}
